<?php
/**
*
* @filename 投资调查
*/
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Rank as Ranks;
use app\admin\model\Lookinto;
use think\Db;

class Rank extends Common
{
    private $cModel;   //当前控制器关联模型
    
    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Ranks;   //别名：避免与控制名冲突
    }
    
    public function index()
    {
        $where = [];
        $dataList = $this->cModel->where($where)->order(['points asc'])->paginate('', false, page_param());
        $this->assign('dataList', $dataList);
				
        return $this->fetch();
    }
	
	public function create(){
        if (request()->isPost()){
            Db::startTrans();
            try{
                $data = input('post.');
                $result = $this->cModel->allowField(true)->save($data);
                $uid = $this->cModel->getLastInsID();
                // 提交事务
                if ($result && $uid){
                    Db::commit();
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajaxReturn($e->getMessage());
            }
        }else{
            return $this->fetch('create');
        }
	}

    public function edit($id)
    {
        if (request()->isPost()){
            $data = input('post.');
			//修改信息
			if (count($data) == 2){
				foreach ($data as $k =>$v){
					$fv = $k!='id' ? $k : '';
				}
				$result = $this->cModel->allowField(true)->save($data, $data['id']);
			}else{
				$result = $this->cModel->allowField(true)->save($data, $data['id']);
			}
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            if ($id > 0){
                $data = $this->cModel->get($id);
                $this->assign('data', $data);
                return $this->fetch();
            }
        }
    }
	
    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                Db::startTrans();
                try{
                    $id_arr = explode(',', $id);   //用户数据
                    $where = [ 'id' => ['in', $id_arr] ];
                    $result = $this->cModel->where($where)->delete();   //删除主表数据
                    // 提交事务
                    if ($result){
                        Db::commit();
                        return ajaxReturn(lang('action_success'), url('index'));
                    }else{
                        return ajaxReturn($this->cModel->getError());
                    }
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return ajaxReturn($e->getMessage());
                }
            }
        }
    }
	
	public function rlist(){

		$lookModel = new Lookinto();
		$where = [];
		if(input('name')){
			$where['a.name'] = ['eq',input('name')];
		}
        if (input('get.search')){
            $where['a.score|b.username|b.mobile'] = ['like', '%'.input('get.search').'%'];
        }
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
		
		if(input('get.down')){
			$tableHeader = ['会员名称','手机号','分数','投资类型','调查时间'];
			$dataList =  $lookModel
						->field('a.*,b.username,b.mobile')
						->alias('a')
						->join('tf_member b','a.uid=b.id','LEFT')
						->where($where)
						->order($order)
						->select();
			$data = [];
			foreach($dataList as $k=>$v){
				$data[$k]['username'] =  $v['username']; 
				$data[$k]['mobile'] =  $v['mobile']; 
				$data[$k]['score'] =  $v['score']; 
				$data[$k]['name'] =  $v['name']; 
				$data[$k]['create_time'] =  $v['create_time']; 
			}
			ToPHPEXECL($tableHeader,$data,'调查列表');
		}
		
		$dataList = $lookModel
					->field('a.*,b.username,b.mobile')
					->alias('a')
					->join('tf_member b','a.uid=b.id','LEFT')
					->where($where)
					->order($order)
					->paginate('', false, page_param());
		$this->assign('dataList', $dataList);
		
		$rankList = $this->cModel->order(['points asc'])->select();
		$this->assign('rankList', $rankList);
		return $this->fetch();
	}
}